﻿using System.Reflection;
using System.Runtime.Loader;

public class PluginLoadContext : AssemblyLoadContext
{
    private readonly string _pluginPath;

    public PluginLoadContext(string pluginPath) :base(isCollectible: true)
    {
        _pluginPath = pluginPath;
        this.Unloading += (context) => OnUnloading(context);
    }

    private void OnUnloading(AssemblyLoadContext context)
    {
        // 在这里执行卸载前的清理工作
        Console.WriteLine("CustomAssemblyLoadContext is unloading.");
        // 例如，释放资源、保存状态等
    }

    protected override System.Reflection.Assembly Load(AssemblyName assemblyName)
    {
        // 尝试从默认加载上下文加载依赖项
        return Default.LoadFromAssemblyName(assemblyName);
    }

    public System.Reflection.Assembly LoadPlugin()
    {
        // 加载插件程序集
        return LoadFromAssemblyPath(System.IO.Path.Combine(_pluginPath, "WpfLibrary1.dll"));
    }

    

}